package com.mybatis.service.sys.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.mybatis.basic.entity.AjaxJson;
import com.mybatis.dao.sys.SysAdminMenuDao;
import com.mybatis.entity.Menu;
import com.mybatis.service.sys.SysAdminMenuService;

@Service
public class SysAdminMenuServiceImpl implements SysAdminMenuService {

	private static final Logger log = Logger.getLogger(SysAdminMenuService.class);

	@Resource
	private SysAdminMenuDao sysAdminMenuDao;

//	@Override
//	public AjaxJson menuManage() {
//		AjaxJson ajaxJson = new AjaxJson();
//		try {
//			List<com.mybatis.dto.Menu> menus = sysAdminMenuDao.menuManage();
//			for (com.mybatis.dto.Menu menu : menus) {
//				System.out.println(menu.toString());
//			}
//			ajaxJson.setSuccess(true);
//			ajaxJson.setMsg("获取成功");
//			ajaxJson.setObj(menus);
//		} catch (Exception e) {
//			e.printStackTrace();
//			ajaxJson.setSuccess(true);
//			ajaxJson.setMsg("获取成功");
//		}
//		return ajaxJson;
//	}

	@Override
	public AjaxJson loadAdminMenu(int adminId) {
		AjaxJson ajaxJson = new AjaxJson();
		List<HashMap<String, Object>> list = sysAdminMenuDao.loadAdminMenu(adminId);
		ajaxJson.setObj(eachMenu(0, list));
		return ajaxJson;
	}

	@Override
	public AjaxJson loadSuperAdminMenu() {
		AjaxJson ajaxJson = new AjaxJson();
		List<HashMap<String, Object>> list = sysAdminMenuDao.loadSuperAdminMenu();
		ajaxJson.setObj(eachMenu(0, list));
		return ajaxJson;
	}

	//循环菜单
	private List<HashMap<String, Object>> eachMenu(int parentId,List<HashMap<String, Object>> list){
		List<HashMap<String, Object>> result = new ArrayList<>();
		for (HashMap<String, Object> t : list) {
			
			int parent_id = Integer.valueOf(t.get("parent_id").toString());
			int id = Integer.valueOf(t.get("id").toString());

			if(parent_id==parentId){
				t.put("children", eachMenu(id, list));
				result.add(t);
			}
		}
		return result;
	}

	@Override
	public AjaxJson addMenu(Menu menu) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			menu.setDel(false);
			if(menu.getLevel()==1){
				menu.setParentId(0);
			}
			int id = sysAdminMenuDao.addMenu(menu);
			System.err.println("id="+id);
			if(id!=0){
				ajaxJson.setSuccess(true);
				ajaxJson.setMsg("添加成功");
			}else{
				ajaxJson.setSuccess(false);
				ajaxJson.setMsg("添加失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("添加失败");
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson selectMenuById(int id) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			Menu menu = sysAdminMenuDao.selectMenuById(id);
			log.info("caidan = "+menu);
			ajaxJson.setObj(menu);
			ajaxJson.setSuccess(true);
			ajaxJson.setMsg("查询成功");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("查询失败");
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson updateMenu(Menu menu) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			int result = sysAdminMenuDao.updateMenu(menu);
			ajaxJson.setSuccess(true);
			ajaxJson.setMsg("修改成功");
			ajaxJson.setObj(result);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("修改失败");
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson deleteMenu(int id) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			int result = sysAdminMenuDao.deleteMenu(id);
			log.info("del count = "+result);
			ajaxJson.setSuccess(true);
			ajaxJson.setMsg("删除成功");
			ajaxJson.setObj(result);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("删除失败");
		}
		return ajaxJson;
	}
}
